﻿using System;
using System.Reflection;

namespace Raider.WinForms.Actions.Linkage
{
    /// <summary>
    /// Marks the action's method as linkable with the event of some client control or component.
    /// </summary>
    [Serializable]
    [AttributeUsage(AttributeTargets.Method)]
    public class LinkWithEventAttribute : LinkAttribute
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LinkWithEventAttribute" /> class.
        /// </summary>
        /// <param name="clientEvent">Name of the event of the client controls and components to link with.</param>
        /// <param name="clientTypes">Supported types of the client controls and components.</param>
        public LinkWithEventAttribute(string clientEvent, params Type[] clientTypes)
            : base(clientTypes)
        {
            ClientEvent = clientEvent;
        }

        /// <summary>
        /// Event name of the client components to link with.
        /// </summary>
        public string ClientEvent
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates Linker object that can be used to link events and methods of the real objects.
        /// </summary>
        /// <param name="actionMethod">Info about the applicable method.</param>
        public override ActionMemberLinker CreateLinker(MemberInfo actionMethod)
        {
            return new ActionEventLinker((MethodInfo)actionMethod, ClientEvent, ClientTypes);
        }

        /// <summary>
        /// Returns an array of the <see cref="LinkWithEventAttribute"/> instances applied to the
        /// specified class member.
        /// </summary>
        public static LinkWithEventAttribute[] GetAttributes(MemberInfo element)
        {
            return (LinkWithEventAttribute[])GetCustomAttributes(element,
                typeof(LinkWithEventAttribute), true);
        }
    }
}
